/*
 * Created by JFormDesigner on Thu Apr 14 20:21:14 CST 2022
 */

package com.ZengQi.event.view;

import com.ZengQi.event.controller.UserInformationController;
import com.ZengQi.event.entity.Comment;
import com.ZengQi.event.entity.Finish;
import com.ZengQi.event.entity.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author QI Zeng
 */
public class UpdateUserInformationView extends JFrame {
    private final String oldUserName;
    private UserInformationController userInformationController=new UserInformationController();
    boolean judge;
    private User user=null;
    public UpdateUserInformationView(String userName) {
        this.oldUserName=userName;
        initComponents();
       User user=userInformationController.getUserInformation(userName);
       userNameTxt.setText(user.getUserName());
       pnumTxt.setText(user.getLoginPnum());
       birthTxt.setText(user.getUserBirthday());
       sexTxt.setText(user.getUserSex());
        setVisible(true);
        setLocationRelativeTo(null);

    }



    private void updateBtn(ActionEvent e) {
        updateBtnActionPerformed(e);
    }

    private void updateBtnActionPerformed(ActionEvent e) {
        if(userNameTxt.getText()==null||"".equals(userNameTxt.getText().trim())||sexTxt.getText()==null||"".equals(sexTxt.getText().trim())||birthTxt.getText()==null||"".equals(birthTxt.getText().trim())){
            JOptionPane.showMessageDialog(this, "各项数据不能为空！");

        }else {
            //如果改了名字则判断是否重名
            if (!userNameTxt.getText().equals(oldUserName)) {
                User conditionUser = new User();
                conditionUser.setUserName(userNameTxt.getText());
                if (userInformationController.userNameIsExit(conditionUser)) {
                    /*
                    提示数据库中已经存在了该昵称
                     */
                    JOptionPane.showMessageDialog(null, "该昵称已存在！");
                    return;
                } else {
                    //不重名则直接修改
                    this.alter();
                }
            } else {
                //没改名也直接修改
                this.alter();
            }


        }

    }

    private void alter() {
        User user=new User();
        user.setUserName(userNameTxt.getText());
        user.setUserSex(sexTxt.getText());
        user.setUserBirthday(birthTxt.getText());

        Finish finish=new Finish();
        finish.setFinisher(userNameTxt.getText());

        Comment comment=new Comment();
        comment.setCommentPublisher(userNameTxt.getText());

        judge=userInformationController.update(user,oldUserName,finish,oldUserName,comment,oldUserName);
        if(judge){
            JOptionPane.showMessageDialog(null, "修改成功！");
            dispose();
        }else {
            JOptionPane.showMessageDialog(null, "数据库出现了问题，请联系管理员，修改失败了~！");
            dispose();
        }

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        pnumLabel = new JLabel();
        pnumTxt = new JTextField();
        userNameLabel = new JLabel();
        userNameTxt = new JTextField();
        sexLabel = new JLabel();
        sexTxt = new JTextField();
        birthLabel = new JLabel();
        birthTxt = new JTextField();
        updateBtn = new JButton();

        //======== this ========
        setIconImage(new ImageIcon(getClass().getResource("/bang.png")).getImage());
        setTitle("\u505a\u9898\u5e2e");
        Container contentPane = getContentPane();

        //======== panel1 ========
        {

            //---- pnumLabel ----
            pnumLabel.setText("\u624b\u673a\u53f7");

            //---- pnumTxt ----
            pnumTxt.setEditable(false);

            //---- userNameLabel ----
            userNameLabel.setText("\u7528\u6237\u540d");

            //---- sexLabel ----
            sexLabel.setText("\u6027\u522b");

            //---- birthLabel ----
            birthLabel.setText("\u751f\u65e5");

            //---- updateBtn ----
            updateBtn.setText("\u786e\u8ba4\u4fee\u6539");
            updateBtn.addActionListener(e -> updateBtn(e));

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(69, 69, 69)
                                .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addComponent(pnumLabel, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(pnumTxt, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE))
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addComponent(sexLabel, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(sexTxt, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)))
                                .addGap(57, 57, 57)
                                .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                    .addComponent(userNameLabel, GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
                                    .addComponent(birthLabel, GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(143, 143, 143)
                                .addComponent(updateBtn, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addComponent(userNameTxt, GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                            .addComponent(birthTxt, GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
                        .addContainerGap(192, Short.MAX_VALUE))
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(pnumLabel)
                            .addComponent(pnumTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(userNameLabel)
                            .addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGap(47, 47, 47)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                            .addComponent(sexLabel)
                            .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(sexTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(birthLabel)
                                .addComponent(birthTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                        .addGap(39, 39, 39)
                        .addComponent(updateBtn)
                        .addContainerGap(42, Short.MAX_VALUE))
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addComponent(panel1, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addComponent(panel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel pnumLabel;
    private JTextField pnumTxt;
    private JLabel userNameLabel;
    private JTextField userNameTxt;
    private JLabel sexLabel;
    private JTextField sexTxt;
    private JLabel birthLabel;
    private JTextField birthTxt;
    private JButton updateBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
